package libScene.src.j2me.m3g;

import javax.microedition.m3g.Appearance;
import javax.microedition.m3g.Mesh;
import javax.microedition.m3g.PolygonMode;
import javax.microedition.m3g.TriangleStripArray;
import javax.microedition.m3g.VertexArray;
import javax.microedition.m3g.VertexBuffer;

public class QuadFactory {

    static Mesh MakeXY(int x1, int y1, int x2, int y2, int z, int aColor) {
        return MakeXY((short) x1, (short) y1, (short) x2, (short) y2, (short) z, aColor);
    }

    static Mesh MakeXY(short x1, short y1, short x2, short y2, short z, int aColor) {
        VertexArray va = new VertexArray(4, 3, 2);
        {
            short[] values = {
                x1, y1, z,
                x2, y1, z,
                x2, y2, z,
                x1, y2, z
            };
            va.set(0, 4, values);
        }
        VertexBuffer vb = new VertexBuffer();
        vb.setDefaultColor(aColor);
        vb.setPositions(va, 1.0f, null);
        PolygonMode pm = new PolygonMode();
        pm.setCulling(PolygonMode.CULL_NONE);
        pm.setWinding(PolygonMode.WINDING_CCW);
        Appearance ap = new Appearance();
        ap.setPolygonMode(pm);

        int[] strip = {0, 1, 3, 2};
        int[] lengths = {4};
        TriangleStripArray tsa = new TriangleStripArray(strip, lengths);

        return new Mesh(vb, tsa, ap);

    }

    static Mesh MakeYZ(int x1, int y1, int x2, int y2, int z, int aColor) {
        return MakeYZ((short) x1, (short) y1, (short) x2, (short) y2, (short) z, aColor);
    }

    static Mesh MakeYZ(short y1, short z1, short y2, short z2, short x, int aColor) {
        VertexArray va = new VertexArray(4, 3, 2);
        {
            short[] values = {
                x, y1, z1,
                x, y1, z2,
                x, y2, z2,
                x, y2, z1
            };
            va.set(0, 4, values);
        }
        VertexBuffer vb = new VertexBuffer();
        vb.setDefaultColor(aColor);
        vb.setPositions(va, 1.0f, null);
        PolygonMode pm = new PolygonMode();
        pm.setCulling(PolygonMode.CULL_NONE);
        pm.setWinding(PolygonMode.WINDING_CCW);
        Appearance ap = new Appearance();
        ap.setPolygonMode(pm);

        int[] strip = {0, 1, 3, 2};
        int[] lengths = {4};
        TriangleStripArray tsa = new TriangleStripArray(strip, lengths);

        return new Mesh(vb, tsa, ap);

    }

    static Mesh MakeXZ(int x1, int z1, int x2, int z2, int y, int aColor) {
        return MakeXZ((short) x1, (short) z1, (short) x2, (short) z2, (short) y, aColor);
    }

    static Mesh MakeXZ(short x1, short z1, short x2, short z2, short y, int aColor) {
        VertexArray va = new VertexArray(4, 3, 2);
        {
            short[] values = {
                x1, y, z2,
                x2, y, z2,
                x2, y, z1,
                x1, y, z1
            };
            va.set(0, 4, values);
        }
        VertexBuffer vb = new VertexBuffer();
        vb.setDefaultColor(aColor);
        vb.setPositions(va, 1.0f, null);
        PolygonMode pm = new PolygonMode();
        pm.setCulling(PolygonMode.CULL_NONE);
        pm.setWinding(PolygonMode.WINDING_CCW);
        Appearance ap = new Appearance();
        ap.setPolygonMode(pm);

        int[] strip = {0, 1, 3, 2};
        int[] lengths = {4};
        TriangleStripArray tsa = new TriangleStripArray(strip, lengths);

        return new Mesh(vb, tsa, ap);

    }
}